﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Mime;
using System.Text;

namespace IrishSettings
{
    public class SettingsValues : Dictionary<string, GenericSetting>
    {
        public void ValidateAndAdd(string key, GenericSetting setting)
        {
            if (setting.Schema.ValueType == null)
            {
                throw new InvalidOperationException("Settings require value type, else we can't deserialize them.");
            }
            Add(key, setting);
        }
    }

    public class SettingsSchema : Dictionary<string, GenericSettingSchema>
    {
        //public SettingsSchema()
        //{
        //    Application= new SettingsApplication();
        //}

        //public SettingsApplication Application { get; set; }
        
        public void Validate()
        {


            foreach (GenericSettingSchema item in Values)
            {
                if (string.IsNullOrEmpty(item.Name))
                {
                    throw new InvalidOperationException("all items in settings schema require name");
                }

                if (item.IsUser && item.Scope == SettingScope.Application)
                {
                    throw new InvalidOperationException("error with " + item.Name + " : Application scope invalid if IsUserSetting is true");
                }

                if (item.IsAnonymous && item.Scope == SettingScope.Application)
                {
                    throw new InvalidOperationException("error with " + item.Name + " : Application scope invalid if IsValidForAnonymous is true");
                }

                if (item.ValueType == null)
                {
                    throw new InvalidOperationException("error with " + item.Name + " : value type is required, e.g. System.String, System.DateTime, etc.");
                }
            }
        }

        public void ValidateAndAdd(string key, GenericSettingSchema setting)
        {
            if (key != setting.Name)
            {
                throw new InvalidOperationException("Key and setting name must match");
            }

            if (setting.ValueType == null)
            {
                throw new InvalidOperationException("Settings require value type, else we can't deserialize them.");
            }
            Add(key, setting);
        }

        public GenericSettingSchema StringItem(string name, string defaultValue, CultureInfo culture)
        {
            //if (string.IsNullOrEmpty(Application.Application))
            //{
            //    throw new InvalidOperationException("You need to set the ApplicationName first");
            //}
            //if (Application.Version == null)
            //{
            //    throw new InvalidOperationException("You need to set the Version number first");
            //}
            GenericSettingSchema item = new GenericSettingSchema();
            //item.VersionObject = Version;
            item.ValueTypeObject = typeof(string);
            item.Name = name;
            item.DefaultSerializedValue = defaultValue;
            item.Scope = SettingScope.Application;
            return item;
        }

        public GenericSettingSchema IntItem(string name, int defaultValue, CultureInfo culture)
        {
            //if (string.IsNullOrEmpty(Application.Application))
            //{
            //    throw new InvalidOperationException("You need to set the ApplicationName first");
            //}
            //if (Application.Version == null)
            //{
            //    throw new InvalidOperationException("You need to set the Version number first");
            //}
            GenericSettingSchema item = new GenericSettingSchema();
            item.ValueTypeObject = typeof(int);
            item.Name = name;
            item.DefaultSerializedValue = defaultValue.ToString("g");
            item.Scope = SettingScope.Application;
            return item;
        }

        public GenericSettingSchema DateItem(string name, DateTime defaultValue, CultureInfo culture)
        {
            //if (Application.Culture == null)
            //    throw new InvalidOperationException("Culture required for any schema containing a DateTime value");

            //if (string.IsNullOrEmpty(Application.Application))
            //{
            //    throw new InvalidOperationException("You need to set the ApplicationName first");
            //}
            //if (Application.Version == null)
            //{
            //    throw new InvalidOperationException("You need to set the Version number first");
            //}
            GenericSettingSchema item = new GenericSettingSchema();
            item.ValueTypeObject = typeof(DateTime);
            item.Name = name;
            item.DefaultSerializedValue = defaultValue.ToString("g", culture.DateTimeFormat);
            item.Scope = SettingScope.Application;
            return item;
        }
    }
}
